/**
 * @about 公共请求拦截
 */
import axios from "axios";

const service: any = axios.create({
    baseURL: "xxx",
    timeout: 30000,
    headers: {
        "Content-Type": "application/json",
    },
});

// 请求拦截---请求前
service.interceptors.request.use(
    (config: any) => {
        let change_loading: boolean = true; //是否 启用加载动画 见 demo

        if (config.params) {
            if (config.params.loading) {
                change_loading = false;
                delete config.params["loading"];
            }
        }
        if (config.data) {
            if (config.data.loading) {
                change_loading = false;
                delete config.data["loading"];
            }

            if (config.data.form_data_query) {
                delete config.data["form_data_query"];
                let new_form_data = new FormData();
                for (let i in config.data) {
                    new_form_data.append(i, config.data[i]);
                    delete config.data[i];
                }
                config.data = new_form_data;
            }
        }

        if (config.url.indexOf("loading") !== -1) {
            config["url"] = config["url"].replace("loading", "");
            change_loading = false;
        }

        if(change_loading) console.log('loading...')

        //添加 token 根据实际情况 设置添加条件
        // config.headers.Authorization = Cookies.get("token");
        return config;
    },
    (error: any) => {
    }
);

// 请求拦截---请求后
service.interceptors.response.use(
    (response: any) => {
        const res = response.data;
        return res;
    },
    (error: any) => {

    }
);

export default service;
